package com.yidu.service.impl;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.yidu.comment.Tools;
import com.yidu.dao.SnAuditMapper;
import com.yidu.dao.SnFiDetailMapper;
import com.yidu.dao.SnFinanceMapper;
import com.yidu.dao.SnMaDetailMapper;
import com.yidu.dao.SnMaInDetailMapper;
import com.yidu.dao.SnMaInventoryMapper;
import com.yidu.dao.SnMaPurDetailMapper;
import com.yidu.dao.SnMaPurchaseMapper;
import com.yidu.dao.SnMaQualityMapper;
import com.yidu.dao.SnMaterialsMapper;
import com.yidu.model.SnAudit;
import com.yidu.model.SnFiDetail;
import com.yidu.model.SnFinance;
import com.yidu.model.SnMaDetail;
import com.yidu.model.SnMaPurDetail;
import com.yidu.model.SnMaPurchase;
import com.yidu.model.SnMaQuality;
import com.yidu.model.SnMaterials;
import com.yidu.model.SnProDetail;
import com.yidu.model.SnUser;
import com.yidu.service.SnMaPurchaseService;
/**
 * 材料采购实现类
 * @author chensituo
 * @date 2018年12月4日 上午10:13:36
 */
@Service
public class SnMaPurchaseServiceImpl implements SnMaPurchaseService{
	/**
	 * 注入材料采购mapper
	 */
	@Resource
	SnMaPurchaseMapper maPurchaseMapper;
	/**
	 * 注入材料采购详情mapper
	 */
	@Resource
	SnMaPurDetailMapper maPurDetailMapper;

	/**
	 * 注入材料mapper
	 */
	@Resource
	SnMaterialsMapper materialsMapper;
	/**
	 * 注入审核表
	 */
	@Resource
	SnAuditMapper auditMapper;
	/**
	 * 注入材料库存mapper
	 */
	@Resource
	SnMaInventoryMapper inventoryMapper;
	/**
	 * 注入材料库存记录mapper
	 */
	@Resource
	SnMaInDetailMapper maInDetailMapper;
	/**
	 * 注入财务mapper
	 */
	@Resource
	SnFinanceMapper financeMapper;
	/**
	 * 注入财务记录mapper
	 */
	@Resource
	SnFiDetailMapper fiDetailMapper;
	
	/**
	 * 注入采购质检mapper
	 */
	@Resource
	SnMaQualityMapper maQualityMapper;
	/**
	 * 注入采购质检详情mapper
	 */
	@Resource
	SnMaDetailMapper maDetailMapper;
	
	
	@Override
	public List<SnProDetail> findAll(Map<String, Object> maps) {
		//调用查询所有的方法
		return maPurchaseMapper.findAll(maps);
	}

	@Override
	public int findCount(Map<String, Object> maps) {
		//调用查询行数的方法
		return maPurchaseMapper.findCount(maps);
	}

	@Override
	public int delete(String purId) {
		//根据id查询
		SnMaPurchase maPurchase=maPurchaseMapper.selectByPrimaryKey(purId);
		maPurchase.setIsva(0);
		//调用删除方法
		return maPurchaseMapper.updateByPrimaryKeySelective(maPurchase);
	}

	@Override
	public int addPurchaseDetail(String sum, String maxCount, String maxMoney, SnUser user,String purId) {
		
		//根据总公司id查询总公司的财务得到财务model
		SnFinance finance=financeMapper.selectBraId("123");
		//如果采购金额超过财务金额
		if(Integer.valueOf(maxMoney)>finance.getFinMoney().intValue()) {
			return 2;
		}
		
		
		int row=0;
		int rows=0;
		int derows=0;
		//如果采购id不为空
		if(purId!=null && !"".equals(purId)) {
			//修改
			//调用采购订单根据id查询的方法
			SnMaPurchase maPurchase=maPurchaseMapper.selectByPrimaryKey(purId);
			//将金额赋值
			maPurchase.setPurMoney(new BigDecimal(maxMoney));
			//将数量赋值
			maPurchase.setPurCount(Integer.valueOf(maxCount));
			maPurchase.setOptime(Tools.getCurDateTime());
			//调用材料采购修改的方法
			row=maPurchaseMapper.updateByPrimaryKeySelective(maPurchase);
			
			//根据采购id查询采购详情mapper的方法
			List<SnMaPurDetail> list=maPurDetailMapper.selectPurId(purId);
			//遍历采购详情集合
			for (SnMaPurDetail snMaPurDetail : list) {
				//调用材料采购详情根据id删除的方法
				derows=maPurDetailMapper.deleteByPrimaryKey(snMaPurDetail.getDeId());
			}
			
			//将得到的字符串拆分
			String [] sums=sum.split("@");
			//遍历
			for (String string : sums) {
				//根据逗号拆分
				String [] arr=string.split(",");
				//创建采购详情对象
				SnMaPurDetail detail=new SnMaPurDetail();
				detail.setPurId(purId);
				detail.setMaId(arr[0]);
				
				
				//创建材料采购详情model
				SnMaPurDetail maPurDetail=new SnMaPurDetail();
				//调用原材料根据id查询的方法
				SnMaterials materials=materialsMapper.selectByPrimaryKey(arr[0]);
				
				//将材料采购详情id赋值
				maPurDetail.setDeId(Tools.getDateOrderNo());
				//将材料采购详情采购id赋值
				maPurDetail.setPurId(maPurchase.getPurId());
				//将材料采购详情采购id赋值
				maPurDetail.setMaId(arr[0]);
				//将材料采购详情数量赋值
				maPurDetail.setDeNumber(new BigDecimal(arr[1]));
				//将材料采购详情单价
				maPurDetail.setDeMoney(materials.getMaMoney().toString());
				//将材料采购详情小计
				maPurDetail.setDeSum(new BigDecimal(materials.getMaMoney().intValue()*Integer.valueOf(arr[1])));
				//将材料采购详情是否有效赋值
				maPurDetail.setIsva(1);
				//将材料采购详情操作时间赋值
				maPurDetail.setOptime(Tools.getCurDateTime());
				//将材料采购详情排序
				maPurDetail.setSort(Integer.valueOf(Tools.getTimeStamp()));
				//调用采购材料详情的增加的方法
				rows=maPurDetailMapper.insertSelective(maPurDetail);
			}


		}else {
			//增加
			//创建采购订单对象 
			SnMaPurchase maPurchase=new SnMaPurchase();
			//将采购订单id赋值
			maPurchase.setPurId(Tools.getDateOrderNo());
			//将采购订单用户id赋值
			maPurchase.setUserId(user.getUserId());
			//将采购订单金额赋值
			maPurchase.setPurMoney(new BigDecimal(maxMoney));
			//将采购订单数量赋值
			maPurchase.setPurCount(Integer.valueOf(maxCount));
			//将采购订单是否有效赋值
			maPurchase.setIsva(1);
			//将采购订单操作人赋值0
			maPurchase.setOper("0");
			//将采购订单操作时间赋值
			maPurchase.setOptime(Tools.getCurDateTime());
			//将采购订单排序赋值
			maPurchase.setSort(Integer.valueOf(Tools.getTimeStamp()));
			//调用增加采购订单的方法
			row=maPurchaseMapper.insertSelective(maPurchase);
			//将得到的字符串拆分
			String [] sums=sum.split("@");
			//遍历
			for (String string : sums) {
				//根据逗号拆分
				String [] arr=string.split(",");
				//创建采购详情对象
				SnMaPurDetail maPurDetail=new SnMaPurDetail();
				//将采购详情id赋值
				maPurDetail.setDeId(Tools.getDateOrderNo());
				//将采购详情采购id赋值
				maPurDetail.setPurId(maPurchase.getPurId());
				//将采购详情材料id赋值
				maPurDetail.setMaId(arr[0]);
				//调用原材料根据id查询的方法
				SnMaterials materials=materialsMapper.selectByPrimaryKey(arr[0]);

				//将采购详情数量赋值
				maPurDetail.setDeNumber(new BigDecimal(arr[1]));
				//将采购详情单价赋值
				maPurDetail.setDeMoney(materials.getMaMoney().toString());
				//将采购详情小计赋值
				maPurDetail.setDeSum(new BigDecimal(materials.getMaMoney().intValue()*Integer.valueOf(arr[1])));
				//将采购详情是否有效赋值
				maPurDetail.setIsva(1);
				//将采购详情操作时间赋值
				maPurDetail.setOptime(Tools.getCurDateTime());
				//将采购详情排序赋值
				maPurDetail.setSort(Integer.valueOf(Tools.getTimeStamp()));
				//调用原材料详情的增加的方法
				rows=maPurDetailMapper.insertSelective(maPurDetail);
			}
		}

		if(row==1 && rows==1) {
			return 1;
		}else {
			return 0;
		}

	}

	@Override
	public Map<String, Object> findByprichaseDetailId(String purId) {
		//根据id查询材料采购订单
		SnMaPurchase maPurchase=maPurchaseMapper.selectByPrimaryKey(purId);
		//根据采购id查询采购详情 得到集合
		List<SnMaPurDetail> list=maPurDetailMapper.selectPurId(purId);
		//创建map
		Map<String, Object> map=new HashMap<>();
		//将采购订单封装
		map.put("maPurchase", maPurchase);
		//将订单详情集合封装
		map.put("list", list);
		return map;
	}

	@Override
	public int updateAudit(String purId) {
		//调用材料采购根据采购材料id查询的方法
		SnMaPurchase maPurchase=maPurchaseMapper.selectByPrimaryKey(purId);
		//将操作人赋值1
		maPurchase.setOper("1");
		//调用材料采购修改的方法
		int rows=maPurchaseMapper.updateByPrimaryKeySelective(maPurchase);
		//创建审核表对象
		SnAudit audit=new SnAudit();
		//将审核业务id赋值
		audit.setAllId(purId);
		//将审核操作时间赋值
		audit.setOptime(Tools.getCurDateTime());
		//将审核状态赋值
		audit.setAuState(0);
		//将审核备注赋值
		audit.setAuComment("这是原材料采购的审核");
		//将审核id赋值
		audit.setAuId(Tools.getDateOrderNo());
		//调用审核表增加的方法
		int row=auditMapper.insert(audit);
		if(row==1 && rows==1) {
			return 1;
		}else {
			return 0;
		}
	}

	@Override
	public int updateState(String purId, String number) {
		//调用材料采购根据id查询的方法
		SnMaPurchase maPurchase=maPurchaseMapper.selectByPrimaryKey(purId);
		//修改数量
		maPurchase.setOper(number);
		//调用修改的方法
		return maPurchaseMapper.updateByPrimaryKeySelective(maPurchase);
	}

	@Override
	public int accomplish(String purId) {
		//调用材料采购根据id查询的方法
		SnMaPurchase maPurchase=maPurchaseMapper.selectByPrimaryKey(purId);
		//修改oper
		maPurchase.setOper("4");
		//调用材料采购修改的方法
		int rows=maPurchaseMapper.updateByPrimaryKeySelective(maPurchase);

		//根据总公司id查询总公司的财务
		SnFinance finance=financeMapper.selectBraId("123");
		//减少总公司财务
		finance.setFinMoney(new BigDecimal(finance.getFinMoney().intValue()-maPurchase.getPurMoney().intValue()));
		//调用修改总公司财务的方法
		int a=financeMapper.updateByPrimaryKeySelective(finance);

		//创建财务记录对象
		SnFiDetail fiDetail=new SnFiDetail();
		//将财务记录对象id赋值
		fiDetail.setFdId(Tools.getDateOrderNo());
		//将财务记录对象财务id赋值
		fiDetail.setFinId(finance.getFinId());
		//将财务记录对象金额赋值
		fiDetail.setFdMoney(new BigDecimal(maPurchase.getPurMoney().intValue()));
		//将财务记录对象支出记录赋值
		fiDetail.setFdExplain("采购材料减少财务金额"+maPurchase.getPurMoney()+"元");
		//将财务记录对象是否有效赋值
		fiDetail.setIsva(1);
		//将财务记录对象操作时间赋值
		fiDetail.setOptime(Tools.getCurDateTime());
		//将财务记录对象排序赋值
		fiDetail.setSort(Integer.valueOf(Tools.getTimeStamp()));

		//调用增加财务记录的方法
		int firows=fiDetailMapper.insertSelective(fiDetail);
		if(rows==1 && a==1 && firows==1)  {
			return 1;
		}else {
			return 0;
		}
		
	}

	@Override
	public int addMaQuality(String purId) {
		
		//材料采购根据采购id查询
		SnMaPurchase maPurchase=maPurchaseMapper.selectByPrimaryKey(purId);
		//将oper赋值为5
		maPurchase.setOper("5");
		//调用材料采购修改的方法
		int a=maPurchaseMapper.updateByPrimaryKeySelective(maPurchase);
		
		//创建材料采购质检对象
		SnMaQuality maQuality=new SnMaQuality();
		//将材料采购质检质检id赋值
		maQuality.setMqId(Tools.getDateOrderNo());
		//将材料采购质检总数量赋值
		maQuality.setMqCount(maPurchase.getPurCount());
		//将材料采购质检采购id赋值
		maQuality.setPurId(purId);
		//将材料采购质检未通过总数量赋值
		maQuality.setMqNotcount(0);
		//将材料采购质检通过同数量赋值
		maQuality.setMqAlready(0);
		//将材料采购质检通过概率赋值
		maQuality.setMqProbability("0%");
		//将材料采购质检是否有效赋值
		maQuality.setIsva(1);
		//将材料采购质检oper赋值0
		maQuality.setOper("0");
		//将材料采购质检操作时间赋值
		maQuality.setOptime(Tools.getCurDateTime());
		//将材料采购质检排序
		maQuality.setSort(Integer.valueOf(Tools.getTimeStamp()));
		//调用增加采购质检的方法
		int rows=maQualityMapper.insertSelective(maQuality);
		
		//根据采购id查询采购详情 得到集合
		List<SnMaPurDetail> list=maPurDetailMapper.selectPurId(purId);
		int row=0;
		//遍历采购详情集合
		for (SnMaPurDetail snMaPurDetail : list) {
			//调用材料mapper根据材料id查询的方法
			SnMaterials materials=materialsMapper.selectByPrimaryKey(snMaPurDetail.getMaId());
			
			//创建材料采购质检详情对象
			SnMaDetail maDetail=new SnMaDetail();
			//将材料采购质检详情质检详情id赋值
			maDetail.setMdId(Tools.getDateOrderNo());
			//将材料采购质检详情质检id赋值
			maDetail.setMqId(maQuality.getMqId());
			//将材料采购质检详情未通过数量赋值
			maDetail.setMdNotcount(0);
			//将材料采购质检详情通过数量赋值
			maDetail.setMdAlready(0);
			//将材料采购质检详情总数量赋值
			maDetail.setMdCount(snMaPurDetail.getDeNumber().intValue());
			//将材料采购质检详情通过率赋值
			maDetail.setMdProbability("0%");
			//将材料采购质检详情是否有效赋值
			maDetail.setIsva(1);
			//将材料采购质检详情操作时间赋值
			maDetail.setOptime(Tools.getCurDateTime());
			//将材料采购质检详情排序赋值
			maDetail.setSort(Integer.valueOf(Tools.getTimeStamp()));
			//将材料采购质检详情操作人赋值材料名称
			maDetail.setOper(materials.getMaName());
			//调用材料采购质检详情增加的方法
			row=maDetailMapper.insertSelective(maDetail);
			if(row==0) {
				break;
			}
		}
		
		if(rows==1 && row==1 && a==1) {
			return 1;
		}else {
			return 0;
		}
	}

}
